package com.jinzhi.demo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.demo.dto.TestValidDTO;
import com.jinzhi.jzapi.training.pojo.CoursesListVo;
import com.jinzhi.jzweb.dao.*;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.ExpertsService;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.oss.domain.FileDO;
import com.jinzhi.oss.service.OssService;
import com.jinzhi.sys.dao.UserRoleDao;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * <pre>
 * 测试专用
 * </pre>
 * 
 * <small> 2018年1月16日 | Aron</small>
 */
@RestController
@RequestMapping("/test")
//@Profile("!prod")
public class TestController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OssService ossService;
    @Autowired
    private CertificateNewsDao certificateNewsDao;
    @Autowired
    private CertificatesDao certificatesDao;
    @Autowired
    private ConsultationsDao consultationsDao;
    @Autowired
    private CoursesDao coursesDao;
    @Autowired
    private ExpertsDao expertsDao;
    @Autowired
    private OrganizationNewsDao organizationNewsDao;
    @Autowired
    private OrganizationRolesDao organizationRolesDao;
    @Autowired
    private ExaminationResultsDao examinationResultsDao;

    @Autowired
    private  TrainingCertificatesDao trainingCertificatesDao;

    @Autowired
    private TrainingResourcesDao trainingResourcesDao;

    @Autowired
    private SysFileDao sysFileDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private TrainStudentInfoDao trainStudentInfoDao;

    @Autowired
    private ExpertsService expertsService; // 专家表服务类

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationNewsService organizationNewsService;

    /**
     * 上传OSS接口测试类
     *
     * jz_certificate_news
     *
     * @return
     */
    @RequestMapping("/uploads1")
    public Object testOss()throws Exception{
        List<CertificateNewsDO> list= certificateNewsDao.certificateNewListX();
        for(CertificateNewsDO li:list){
            if(StringUtil.notEmpty(li.getCertificateImg())){
                String url[]=li.getCertificateImg().split("upload");
                File file1 =new File("D:/upload"+url[1]);
                System.out.println("url[0]="+url[1]);
                if(file1.exists()){
                    InputStream inputStream = new FileInputStream(file1);
                    MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                    String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                     int i=certificateNewsDao.updateCertificateX(urls,li.getId().toString());
                    System.out.println("urls="+urls);
                }
            }

        }
        return "执行完成";
    }
    /**
     * 上传OSS接口测试类
     *
     * jz_certificates
     *
     * @return
     */
    @RequestMapping("/uploads2")
    public Object testOss1()throws Exception{
        List<CertificatesDO> list= certificatesDao.certificatesListX();
        for(CertificatesDO li:list){
            if(StringUtil.notEmpty(li.getImgUrl())){
                String url[]=li.getImgUrl().split("upload");
                File file1 =new File("D:/upload"+url[1]);
                System.out.println("url[0]="+url[1]);
                if(file1.exists()){
                    InputStream inputStream = new FileInputStream(file1);
                    MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                    String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                   int i= certificatesDao.updateCertificateX(urls,li.getId().toString());
                    System.out.println("urls="+urls);
                }
            }

        }
        return "执行完成";
    }


    /**
     * 上传OSS接口测试类
     *
     * jz_consultations
     *
     * @return
     */
    @RequestMapping("/uploads3")
    public Object testOss3()throws Exception{
        List<ConsultationsDO> list= consultationsDao.consultaionsListX();
        for(ConsultationsDO li:list){

            String urls1=null;
            String urls=null;
            if(StringUtil.notEmpty(li.getThumbnail())){
                String url1[]=li.getThumbnail().split("upload");
                if(url1.length>1){
                    File file2 =new File("D:/upload"+url1[1]);
                    if(file2.exists()){
                        InputStream inputStream = new FileInputStream(file2);
                        MultipartFile multipartFile = new MockMultipartFile(file2.getName(), inputStream);
                        urls1= ossService.uploadUrlFileOss(multipartFile,"upload"+url1[1]);
                        System.out.println("urls1="+urls1);
                    }
                }

            }

            if(StringUtil.notEmpty(li.getFiles())){
                String url[]=li.getFiles().split("upload");
                if(url.length>1){
                    File file1 =new File("D:/upload"+url[1]);
                    System.out.println("url[0]="+url[1]);

                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                        urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);

                        System.out.println("urls="+urls);
                    }
                }


            }
            int i= consultationsDao.updateCertificateX(urls,li.getId().toString(),urls1);
        }

        return "执行完成";
    }
    /**
     * 上传OSS接口测试类
     * jz_courses
     * @return
     */
    @RequestMapping("/uploads4")
    public Object testOss4()throws Exception{
        List<CoursesListVo> list= coursesDao.couresListX();
        for(CoursesListVo li:list){
            if(StringUtil.notEmpty(li.getThumbnail())){
                String url[]=li.getThumbnail().split("upload");
                File file1 =new File("D:/upload"+url[1]);
                System.out.println("url[0]="+url[1]);
                if(file1.exists()){
                    InputStream inputStream = new FileInputStream(file1);
                    MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                    String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                    int i= coursesDao.updatecouresX(urls,li.getId().toString());
                    System.out.println("urls="+urls);
                }
            }
        }
        System.out.println("执行完成4444444444=");
        return "执行完成";
    }

    /**
     * 上传OSS接口测试类
     * jz_experts
     * @return
     */
    @RequestMapping("/uploads5")
    public Object testOss5()throws Exception{
        List<Map> list= expertsDao.ExpListX();
        for(Map li:list){
            if(StringUtil.notEmpty(li.get("enclosure").toString())){
                String url[]=li.get("enclosure").toString().split("upload");
                if(url.length>1){
                    File file1 =new File("D:/upload"+url[1]);

                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                        String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                        int i= expertsDao.updateExpX(urls,li.get("id").toString());
                        System.out.println("urls="+urls);
                    }else {
                        System.out.println("url[0]="+url[1]+"id="+li.get("id"));
                        System.out.println("未找到文件");
                    }
                }

            }
        }
        System.out.println("执行完成=------------5555555");
        return "执行完成";
    }


    /**
     * 上传OSS接口测试类
     * jz_organization_news
     * @return
     */
    @RequestMapping("/uploads6")
    public Object testOss6()throws Exception{
        List<OrganizationNewsDO> list= organizationNewsDao.organizationsListX();
        for(OrganizationNewsDO li:list){
            if(StringUtil.notEmpty(li.getCachet())){
                String url[]=li.getCachet().split("upload");
                File file1 =new File("D:/upload"+url[1]);
                System.out.println("url[0]="+url[1]);
                if(file1.exists()){
                    InputStream inputStream = new FileInputStream(file1);
                    MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                    String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                    int i= organizationNewsDao.updateorganizationsX(urls,li.getId().toString());
                    System.out.println("urls="+urls);
                }
            }
        }
        System.out.println("=执行完毕==============66666666");
        return "执行完成";
    }

    /**
     * 上传OSS接口测试类
     * jz_organization_roles
     * @return
     */
    @RequestMapping("/uploads7")
    public Object testOss7()throws Exception{
        List<Map> list= organizationRolesDao.orgRolesListX();
        for(Map li:list){
            if(li.get("enclosure")!=null){

            if(StringUtil.notEmpty(li.get("enclosure").toString())){
                String url[]=li.get("enclosure").toString().split("upload");
                File file1 =new File("D:/upload"+url[1]);
                System.out.println("url[0]="+url[1]);
                if(file1.exists()){
                    InputStream inputStream = new FileInputStream(file1);
                    MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                    String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                    int i= organizationRolesDao.updateorgRolesX(urls,li.get("id").toString());
                    System.out.println("urls="+urls);
                }
            }
            }
        }
        System.out.println("=执行完毕==============77777777777");
        return "执行完成";
    }

    /**
     * 上传OSS接口测试类
     * jz_stu_sign_up
     * @return
     */
    @RequestMapping("/uploads8")
    public Object testOss8()throws Exception{
        List<Map> list= examinationResultsDao.stusinUpListX();
        for(Map li:list){
            if(li.get("photo")!=null){

                if(StringUtil.notEmpty(li.get("photo").toString())){
                    String url[]=li.get("photo").toString().split("upload");
                    if(url.length>1){
                        File file1 =new File("D:/upload"+url[1]);
                        System.out.println("url[0]="+url[1]);
                        if(file1.exists()){
                            InputStream inputStream = new FileInputStream(file1);
                            MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                            String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                            int i= examinationResultsDao.updatestusinUpX(urls,li.get("id").toString());
                            System.out.println("urls="+urls);
                        }
                    }

                }
            }
        }
        System.out.println("=执行完毕==============88888888");
        return "执行完成";
    }

    /**
     * 上传OSS接口测试类
     * jz_training_certificates
     * @return
     */
    @RequestMapping("/uploads9")
    public Object testOss9()throws Exception{
        List<Map> list= trainingCertificatesDao.rainingListX();
        for(Map li:list){
            String urls1=null;
            String urls=null;
            if(li.get("img_url")!=null){
                if(StringUtil.notEmpty(li.get("img_url").toString())){
                    String url[]=li.get("img_url").toString().split("upload");
                    File file1 =new File("D:/upload"+url[1]);
                    System.out.println("url[0]="+url[1]);
                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                         urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                    }
                }
            }
            if(li.get("img_url_nosale")!=null){
                if(StringUtil.notEmpty(li.get("img_url_nosale").toString())){
                    String url[]=li.get("img_url_nosale").toString().split("upload");
                    File file1 =new File("D:/upload"+url[1]);
                    System.out.println("url[0]="+url[1]);
                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                         urls1= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                        System.out.println("urls="+urls);
                    }
                }
            }
            int i= trainingCertificatesDao.updatetrainingX(urls,li.get("id").toString(),urls1);
        }
        System.out.println("=执行完毕==============999999999");
        return "执行完成";
    }

    /**
     * 未运行
     * 上传OSS接口测试类
     * jz_training_resources
     * @return
     */
    @RequestMapping("/uploads10")
    public Object testOss10()throws Exception{
        List<Map> list= trainingResourcesDao.resourcesListX();
        for(Map li:list){
            if(li.get("file_route")!=null){
                    }
                if(StringUtil.notEmpty(li.get("file_route").toString())){
                        String url[]=li.get("file_route").toString().split("upload");
                    File file1 =new File("D:/upload"+url[1]);
                    String url111=li.get("file_route").toString().substring(li.get("file_route").toString().length()-4);
                    System.out.println("后缀==="+url111);
//                    if(url111.equals(".zip")||url111.equals(".rar")) {
//                        System.out.println("结束当前循环");
//                        continue;
//                    }
                    System.out.println("url[0]="+url[1]);
                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                        String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                        int i= trainingResourcesDao.updateresourcesX(urls,li.get("id").toString());
                        System.out.println("urls="+urls);
                    }
                }
            }
        System.out.println("=执行完毕==============10101010");
        return "执行完成";
    }


    /**
     * 未运行
     * 上传OSS接口测试类
     * jz_training_resources
     * @return
     */
    @RequestMapping("/uploads11")
    public Object testOss11()throws Exception{
        System.out.println("=开始执行OSS导入==============");
        List<FileDO> list= sysFileDao.sysFileListX();
        for(FileDO li:list){

                if(StringUtil.notEmpty(li.getUrl().toString())){
                    String url[]=li.getUrl().split("upload");
                    File file1 =new File("D:/upload"+url[1]);
                    System.out.println("url[0]="+url[1]);
                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                        String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                        int i= sysFileDao.updatesysFilewX(urls,li.getId().toString());
                        System.out.println("urls="+urls);
                    }
                }
        }
        System.out.println("=开始第二个方法的执行==============");
        List<Map>  list1= userRoleDao.sysUserListX();
        for(Map li1:list1){
            if(li1.get("headPortrait")!=null){
                if(StringUtil.notEmpty(li1.get("headPortrait").toString())){
                    String url[]=li1.get("headPortrait").toString().split("upload");
                    if(url.length>1){
                        File file1 =new File("D:/upload"+url[1]);
                        System.out.println("url[0]="+url[1]);
                        if(file1.exists()){
                            InputStream inputStream = new FileInputStream(file1);
                            MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                            String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                            int i= userRoleDao.updatesysUserX(urls,li1.get("id").toString());
                            System.out.println("urls="+urls);
                        }
                    }

                }
            }
        }
        System.out.println("=开始第三个方法的执行==============");
        List<Map> mapList= trainStudentInfoDao.studenInfoListX();
        for(Map ls:mapList){
            if(ls.get("picAddress")!=null){
                if(StringUtil.notEmpty(ls.get("picAddress").toString())){
                    String url[]=ls.get("picAddress").toString().split("upload");
                    File file1 =new File("D:/upload"+url[1]);
                    System.out.println("url[0]="+url[1]);
                    if(file1.exists()){
                        InputStream inputStream = new FileInputStream(file1);
                        MultipartFile multipartFile = new MockMultipartFile(file1.getName(), inputStream);
                        String urls= ossService.uploadUrlFileOss(multipartFile,"upload"+url[1]);
                        int i= trainStudentInfoDao.updatestudenInfoX(urls,ls.get("id").toString());
                        System.out.println("urls="+urls);
                    }
                }
            }
        }

        System.out.println("=全部=======执行完毕==============1111111111111");
        return "执行完成";
    }

    @RequestMapping("/session")
    public void session(HttpServletRequest request) {
        System.out.println("request class : " + request.getClass());
        System.out.println("request session : " + request.getSession());

    }

    @RequestMapping("/redis")
    public void redis() {
        Set<String> keys = stringRedisTemplate.keys("*");
        System.out.println(keys);
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("Aron", "hello Aron.");
        String aron = stringStringValueOperations.get("Aron");
        System.out.println(aron);

        Long expire = stringRedisTemplate.getExpire("Aron");
        Long expire2 = stringRedisTemplate.getExpire("Aron", TimeUnit.MICROSECONDS);
        Long expire3 = stringRedisTemplate.getExpire("Aron", TimeUnit.SECONDS);
        System.out.println(expire);
        System.out.println(expire2);
        System.out.println(expire3);

    }

    @GetMapping("/see")
    public void see() {
        System.out.println("Hello World!");
//        Set<String> keys = stringRedisTemplate.keys("*");
//        System.out.println(keys);
//        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
//        stringStringValueOperations.set("Aron", "hello Aron.");
//        String aron = stringStringValueOperations.get("Aron");
//        System.out.println(aron);
//
//        Long expire = stringRedisTemplate.getExpire("Aron");
//        Long expire2 = stringRedisTemplate.getExpire("Aron", TimeUnit.MICROSECONDS);
//        Long expire3 = stringRedisTemplate.getExpire("Aron", TimeUnit.SECONDS);
//        System.out.println(expire);
//        System.out.println(expire2);
//        System.out.println(expire3);

    }

    @PostMapping("/valid")
    public void valid(@RequestBody TestValidDTO dto) {
        System.out.println(dto);
    }

    /**
     * 填充专家表中没有机构信息的行数据
     */
    @PostMapping("/autoExportExpert")
    public void autoExportExpert(@RequestBody ExpertsDO expertsDO) {
        // 查询专家信息表得出 ids -》条件为没有机构信息的行记录
        List<ExpertsDO> expertsDOList =
                expertsService.selectList(new EntityWrapper<>(new ExpertsDO()).isNull("org_id"));

        for (ExpertsDO e : expertsDOList) {
            // 取出专家表中没有机构信息的用户id，并查询用户表取出机构id
            UserDO userDO = userService.selectById(e.getUserId());
            OrganizationNewsDO organizationNewsDO = new OrganizationNewsDO();
            // 如果没有机构-则增加一条机构信息，机构信息除了机构名称，还需要增加机构类型，机构类型统一为 事业单位/学校
            // 2020-12-14 zyf 修改逻辑，为防止用户随意填写机构信息，当发现机构表中不存在机构，则跳过该条信息的填充
            if (!ObjectUtils.isEmpty(userDO.getOrgId())) {
                // 填充到专家信息表中
                e.setOrgId(userDO.getOrgId());
                expertsService.insertOrUpdate(e);
            } else if (StringUtil.notEmpty(userDO.getOrgName())) {
                List<OrganizationNewsDO> orgDOList =
                        organizationNewsService.selectList(new EntityWrapper<>(
                                new OrganizationNewsDO()).like("name", userDO.getOrgName()));
                // 模糊查询到多个，或者查询不到，都表示机构名称不正确
                if (orgDOList.size() != 1)
                    continue;

                OrganizationNewsDO fillOrgDO = orgDOList.get(0);

                // 更新用户信息
                userDO.setOrgId(fillOrgDO.getId());
                userService.updateById(userDO);

                e.setOrgId(fillOrgDO.getId());
                expertsService.insertOrUpdate(e);
            }
        }
    }
}
